<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/6 0006
 * Time: 17:11
 */
namespace app\modules\cn\models;

use app\modules\app\models\IntegralBuy;
use app\modules\cn\models\ResourcesPhone;
use yii\db\ActiveRecord;

class HotActive extends ActiveRecord
{
    public static function tableName()
    {
        return '{{%hot_active}}';
    }

    public function rules()
    {
        return [
            // username and password are both required
            // [['student', 'cate', '', 'duration'], 'required'],

        ];
    }

    /**
     * [getReservation 计算热门活动报名人数]
     * @Author don
     * @Date   2021-12-15
     * @param  [type]     $data [description]
     * @return [type]           [description]
     */
    public static function getReservation($data)
    {
        foreach ($data as $k => $v) {
            $count = ResourcesPhone::find()->asArray()->where('remark="' . $v['title'].'"')->count();
            $data[$k]['count'] = $v['reservations']+$count;
        }
        return $data;
    }


    /**
     * [getReservation 计算热门活动报名人数]
     * @Author don
     * @Date   2021-12-15
     * @param  [type]     $data [description]
     * @return [type]           [description]
     */
    public static function getSingleReservation($data)
    {
        $count = ResourcesPhone::find()->asArray()->where('remark="' . $data['title'].'"')->count();
        $data['count'] = $data['reservations']+$count;
    
        return $data;
    }

    public static function activeDeal($data,$uid=0){
        foreach ($data as $key => $value) {
            $data[$key]['video'] = 0;
            $data[$key]['isBuy'] = 0;
            $data[$key]['isCheck'] = 0;
            $data[$key]['course_description'] = $value['course_description']?\Yii::$app->params['thinkUrl'].$value['course_description']:'';
            if($uid){
                $tmp = ResourcesPhone::checkResource($uid, $value['title']);
                if ($tmp) {
                    $data[$key]['isCheck'] = 1;//报名
                }
            }
            $data[$key]['count']=$value['reservations'];
            if ($value['startTime'] && $value['endTime']) {
                if (strtotime($value['startTime']) > time()) {
                    $data[$key]['activeStatus'] = 0; // 未开始
                } elseif (strtotime($value['endTime']) < time()) {
                    $data[$key]['activeStatus'] = 2; // 已结束
                } else {
                    $data[$key]['activeStatus'] = 1; // 进行中
                }
            } else {
                $data[$key]['activeStatus'] = 0; // 未开始
            }
            if($value['cate']==1&&trim($value['sdk'])){
                //公开课&&有视频
                $data[$key]['video'] = 1;
                //是否购买
                if($value['free']==1&&$value['integral']>0){
                    //付费，是否购买
                    $data[$key]['free'] = 1;
                    $isBuy = IntegralBuy::videoIsBuyOne($uid,$value['id'],2);
                    if($isBuy){
                        $data[$key]['isBuy'] = 1;
                    }
                }else{
                    $data[$key]['free'] = 0;
                }
            }
        }
        return $data;
    }
}